package com.awifi.com.thread.waiting;

/**
 * @description: 线程的等待唤醒案例，线程之间的通信
 * @author: marin
 * @time: 2020/6/29 9:13
 */
public class ThreadWaiting {
    public static void main(String[] args){
        final Object object = new Object();
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (object) {
                        System.out.println("消费者1需要消费商品的数量");
                        try {
                            object.wait(5000);
                            //Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        //唤醒后执行的代码
                        System.out.println("可以开始消费");
                        System.out.println("------------------");
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                while(true){
                    try{
                        Thread.sleep(5*1000);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    synchronized (object){
                        System.out.println("生产者已经准备好");
                        object.notify();
                    }
                }
            }
        }.start();
    }

}
